package display.controller;

import java.util.Iterator;

import display.structure.Wheel;

import prefuse.data.Tuple;

public class SentiWheelAnimator extends WheelSpeedAnimator{

	public SentiWheelAnimator(String group, Wheel wheel) {
		super(group, 1, wheel);
	}
	private double maxPossibleSentiment = 0;
	private double maxSpeed = 0.2;
	@Override
	public void run(double frac) {
		double totalSent = 0;
		for(Iterator<Tuple> it = wheel.keywordTable.tuples(); it.hasNext();){
			Tuple keyword = it.next();
			try{
				totalSent += keyword.getDouble(Wheel.SENT);
				if (maxPossibleSentiment < Math.abs(totalSent)) {
					maxPossibleSentiment = Math.abs(totalSent);
				}
			}
			catch (ArrayIndexOutOfBoundsException e) {
				//ignore
			}
		}
		if (maxPossibleSentiment < totalSent) {
			maxPossibleSentiment = totalSent;
		}
		if (maxPossibleSentiment == 0) {
			setRadPerSec(0);
		}
		else{
			setRadPerSec(0-maxSpeed * totalSent / maxPossibleSentiment);
//			System.out.println(getRadPerSec());
		}
		maxPossibleSentiment *= 0.99;
		super.run(frac);
		wheel.updateStarLayers();
	}
}
